package com.yanqu.road.server.manager.union.activitygroup;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.logic.bussiness.union.activitygroup.UnionActivityGroupBusiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.server.CrossCenterServer2;
import com.yanqu.road.server.manger.union.BaseUnionActivityGroupMgr;
import com.yanqu.road.utils.ServerType;

import java.util.Map;

public class Cross3UnionActivityGroupMgr extends BaseUnionActivityGroupMgr {

    private static final Object saveLock = new Object();

    public static void setReceiveStatus(int activityId, long serverId, long crossId) {
        Map<Integer, UnionActivityGroup> groupMap = getGroupMap(activityId);
        if (groupMap == null) {
            return;
        }

        for (UnionActivityGroup group : groupMap.values()) {
            if (crossId > 0 && group.getCrossServerId() == crossId) {
                group.setSyncCrossServer(true);
            }
            if (serverId > 0) {
                group.getNoSyncServerSet().remove(serverId); // 不一定有这个区服ID，反正直接remove
            }
        }
    }

    public static int getGroupId(int activityId, String unionUid) {
        ActivityInfo activityInfo = UnionActivityGroupLockMgr.getActivityInfoMap().get(activityId);
        if (activityInfo != null) {
            return getGroupId(activityInfo, unionUid);
        }
        return UnionActivityGroupHelper.NO_GROUP_ID;
    }

    public static int getGroupId(int activityId, long userId) {
        ActivityInfo activityInfo = UnionActivityGroupLockMgr.getActivityInfoMap().get(activityId);
        if (activityInfo != null) {
            return getGroupId(activityInfo, userId);
        }
        return UnionActivityGroupHelper.NO_GROUP_ID;
    }

    public static void saveDbData() {
        synchronized (saveLock) {
            for (Map<Integer, UnionActivityGroup> groupMap : activityGroupMap.values()) {
                for (UnionActivityGroup group : groupMap.values()) {
                    try {
                        // 保存分组
                        if (group.isInsertOption()) {
                            UnionActivityGroupBusiness.addUnionActivityGroup(group);
                        } else if (group.isUpdateOption()) {
                            UnionActivityGroupBusiness.updateUnionActivityGroup(group);
                        }
                    } catch (Exception e) {
                        getLogger().error("error : ", e);
                    }
                    for (UnionActivityGroupUnion union : group.getUnionDataMap().values()) {
                        try {
                            // 保存商会
                            if (union.isInsertOption()) {
                                UnionActivityGroupBusiness.addUnionActivityGroupUnion(union);
                            } else if (union.isUpdateOption()) {
                                UnionActivityGroupBusiness.updateUnionActivityGroupUnion(union);
                            }
                        } catch (Exception e) {
                            getLogger().error("error : ", e);
                        }
                    }
                }
            }
        }
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        loadDbData(ServerType.CENTER_CROSS_2, CrossCenterServer2.getInstance().getServerId());
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        saveDbData();
        return true;
    }
}
